package cn.edu.hnnu.c.rentacar.dao.hibernateimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import cn.edu.hnnu.c.rentacar.beans.Car;
import cn.edu.hnnu.c.rentacar.idao.ICarDAO;
import cn.edu.hnnu.c.rentacar.util.HibernateUtil;

public class CarDAOHibernateImpl implements ICarDAO {
	
	private static CarDAOHibernateImpl instance;
	
	private CarDAOHibernateImpl(){
		
	}
	
	public static CarDAOHibernateImpl getInstance(){
		if(null==instance){
			instance = new CarDAOHibernateImpl();
		}
		return instance;
	}

	@Override
	public boolean addCar(Car car) {//添加车辆到数据库Car表
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.openSession();
		Transaction tran = null;
		try{		
			tran = session.beginTransaction();
			session.save(car);
			tran.commit();	
		}catch(HibernateException e){
			e.printStackTrace();
			tran.rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}
	
	public boolean deleteCarById(int id){//删除汽车
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.openSession();
		Transaction tran = null;
		try{
			tran = session.beginTransaction();
			//Car car = (Car)session.get(Car.class, id);
			Car car1 = new Car();
			car1.setCarId(id);
			session.delete(car1);
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tran.rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean updateCar(Car car) {//更新汽车信息
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.openSession();
		Transaction tran = null;
		try{
			tran = session.beginTransaction();
			session.update(car);
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tran.rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public List<Car> getAllCars() {//查询所有汽车的信息
		List cars = null;
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.openSession();
		Transaction tran = null;
		try{
			tran = session.beginTransaction();
			Query query = session.createQuery("from Car");
			cars = query.list();
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			tran.rollback();
			return null;
		}
		return cars;
	}

	@Override 
	public Car getCarById(int id) {//根据汽车编号查询汽车信息
		Car car = null;
		SessionFactory factory = HibernateUtil.getSessionFactory();
		Session session = factory.openSession();
		Transaction tran = null;
		try{
			tran = session.beginTransaction();
			car = (Car)session.load(Car.class, id);
			tran.commit();
		}catch(HibernateException e){
			e.printStackTrace();
			return null;
		}
		return car;
	}
}
